<template>
    <div class="count">
        <div class="login-count">
            <el-form
                ref="ruleFormRef"
                :model="ruleForm"
                :rules="rules"
                class="login-form">
                <h3 class="title">登录</h3>
                <!-- 用户名 -->
                <el-form-item  prop="username">
                    <el-input class="input-with-select" v-model="ruleForm.username" placeholder="用户名" clearable>
                        <template #prefix>
                        <el-icon><User /></el-icon>
                        </template>
                    </el-input>
                </el-form-item>

                <!-- 密码 -->
                <el-form-item  prop="password">
                    <el-input
                        type="password"
                        v-model="ruleForm.password"
                        class="input-with-select" placeholder="密码" clearable>
                        <template #prefix>
                        <el-icon><Lock /></el-icon>
                        </template>
                    </el-input>
                </el-form-item>

                <!-- 验证码 -->
                <el-form-item prop="captchaCode">
                        <el-input
                            v-model="ruleForm.captchaCode"
                            auto-complete="off"
                            size="large"
                            class="input-with-select"
                            style="width: 49%;"
                            placeholder="验证码">
                            <template #prefix>
                                <svg-icon icon="captcha"/>
                            </template>
                        </el-input>

                        <el-image @click="verify" :src="captchaBase64" style="width: 49%;padding-left: 5px;"/>
                </el-form-item>

                <el-form-item>
                    <el-button :loading="load" type="primary" @click="submitForm(ruleFormRef)" style="width: 100%">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { reactive,onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useLoginStore,usePermissionStore } from '@/stores/index'
import {login,captcha} from '@/api/loginAuth'
import {getMenuList} from '@/api/permission'

const ruleFormRef = ref<FormInstance>()
const router = useRouter();
const load = ref(false);
const userLogin = useLoginStore();
const userPer = usePermissionStore();
const captchaBase64 = ref('')
const ruleForm = reactive({
    username:'',
    password:'',
    captchaCode:'',
    captchaKey:''
})
const rules = reactive<FormRules>({
    username: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
    ],
})

const verify = ()=>{
    captcha().then((res: any) =>{
        console.log('res=',res)
        ruleForm.captchaKey = res.captchaKey
        captchaBase64.value = res.captchaBase64
    }).catch((err: any) =>{
        console.log('请求异常：', err)
        ElMessage.error('验证码加载失败')
    })
}

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid) => {
        if(valid){
            load.value = true;
            login(ruleForm).then((res: any) => {
                userLogin.addUserInfo(res.userVo)
                userLogin.addAccessTokne(res.accessToken)
                // 加载并缓存路由
                getMenuList().then((res: any) => {
                    userPer.addMenuList(res)
                    // 跳转首页
                    router.replace("/dashboard");
                }).catch((error: any) => {
                    console.log('请求异常：', error)
                })
            }).catch((error: any) => {
                console.log('请求异常：', error)
                load.value = false;
            })
        }else{
            ElMessage.error('请填写数据')
        }
    })
}

onMounted(() =>{
    verify()
})
</script>

<style scoped>
.count{
    width: 100vw;
    height: 100vh;
}
.login-count{
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background: url('/src/assets/img/login-background.jpg') 0 0 no-repeat;
    background-size: cover;
    width: 100%;
    overflow: hidden;
}
.login-form{
    border-radius: 15px;
    margin: 180px auto;
    width: 250px;
    padding: 15px 35px 15px 35px;
    background: #ffffff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
}
.title{
    text-align: center;
}
</style>